package com.gitee.wsl.compose.ui.canvaspainter.components

import androidx.compose.animation.animateContentSize
import androidx.compose.foundation.background
import androidx.compose.foundation.clickable
import androidx.compose.foundation.gestures.Orientation
import androidx.compose.foundation.gestures.rememberScrollableState
import androidx.compose.foundation.gestures.scrollable
import androidx.compose.foundation.layout.*
import androidx.compose.material.icons.Icons
import androidx.compose.material.icons.automirrored.filled.Redo
import androidx.compose.material.icons.automirrored.filled.Undo
import androidx.compose.material.icons.filled.Brush
import androidx.compose.material.icons.filled.ClearAll
import androidx.compose.material.icons.filled.Palette
import androidx.compose.material3.Icon
import androidx.compose.material3.IconButton
import androidx.compose.runtime.Composable
import androidx.compose.runtime.collectAsState
import androidx.compose.runtime.mutableStateOf
import androidx.compose.runtime.remember
import androidx.compose.ui.Modifier
import androidx.compose.ui.graphics.Color
import androidx.compose.ui.unit.dp
import com.gitee.wsl.compose.icon.InkEraser
import com.gitee.wsl.compose.icon.InkEraserOff
import com.gitee.wsl.compose.ui.canvaspainter.PainterController
import com.gitee.wsl.compose.ui.canvaspainter.PensSection


@Composable
internal fun PainterToolbar(
    modifier: Modifier = Modifier,
    painterController: PainterController
) {

    val isColorSelection = remember {
        mutableStateOf(false)
    }

    val isStrokeSelection = painterController.isStrokeSelection.collectAsState()
    val selectedColor = painterController.selectedColor.collectAsState()
    val undonePath = painterController.undonePath.collectAsState()
    val paintPath = painterController.paintPath.collectAsState()

    val isEraserActive = painterController.isEraserActive.collectAsState()

    Column(
        Modifier.fillMaxWidth()
            .background(Color.Gray.copy(alpha = 0.5f))
            .animateContentSize()
            .scrollable(
                enabled = isColorSelection.value,
                orientation = Orientation.Vertical,
                // Scrollable state: describes how to consume
                // scrolling delta and update offset
                state = rememberScrollableState { delta ->
                    if (delta < -20) {
                        isColorSelection.value = false
                    }
                    delta
                }
            )
    ) {
        if (isColorSelection.value)
            PensSection(painterController = painterController)
        Row(
            modifier
                .fillMaxWidth(),
            horizontalArrangement = Arrangement.SpaceBetween,
        ) {
            if (isStrokeSelection.value) {
                StrokeSelector(
                    minStrokeWidth = painterController.minStrokeWidth,
                    maxStrokeWidth = painterController.maxStrokeWidth,
                    strokeWidth = painterController.strokeWidth.collectAsState().value,
                    selectedColor = painterController.selectedColor.collectAsState().value,
                    onStrokeWidthUpdate = painterController::setStrokeWidth,
                    toggleStrokeSelection = painterController::toggleStrokeSelection,
                )
            } else {
                Row {
                    //Eraser
                    IconButton(
                        onClick ={ painterController.changeEraserState() }
                    ){
                        Icon( if (isEraserActive.value) Icons.InkEraser else Icons.InkEraserOff,
                            "eraser",
                            tint = if (isEraserActive.value) Color.Red else Color.Gray,
                        )
                    }

//                    Stroke Selection
                    IconButton(onClick = {
                        painterController.toggleStrokeSelection()
                    }) {
                        Icon(Icons.Default.Brush,"stroke")
                        //Icon(painterResource(Res.drawable.ic_baseline_adjust_24), "stroke")
                    }

//                    Color Selection
                    IconButton(onClick = {
                        isColorSelection.value = isColorSelection.value.not()
                    }) {
                        Icon(
                            imageVector = Icons.Default.Palette,
                            tint = selectedColor.value.color,
                            contentDescription = "color"
                        )
//                        Image(
//                            painterResource(Res.drawable.ic_baseline_brush_24,),
//                            "color",
//                            colorFilter = ColorFilter.tint(selectedColor.value.color)
//                        )
                    }
//                    Undo
                    IconButton(
                        onClick = {
                            painterController.undo()
                        },
                        enabled = paintPath.value.isNotEmpty(),
                    ) {
                        Icon(imageVector = Icons.AutoMirrored.Filled.Undo, contentDescription = "undo")
                        //Icon(painterResource(Res.drawable.ic_round_undo_24), "undo")
                    }
//                    Redo
                    IconButton(
                        onClick = {
                            painterController.redo()
                        },
                        enabled = undonePath.value.isNotEmpty(),
                    ) {
                        Icon(Icons.AutoMirrored.Filled.Redo, "redo")
                        //Icon(painterResource(Res.drawable.ic_round_redo_24), "redo")
                    }

//                    Redo
                    IconButton(
                        onClick = {
                            painterController.reset()
                        },
                        enabled = paintPath.value.isNotEmpty(),
                    ) {
                        Icon(Icons.Filled.ClearAll, "clear")
                        //Icon(painterResource(Res.drawable.ic_baseline_clear_all_24), "redo")
                    }
                }
            }
        }
    }

}